package com.example.consumer.controller;

import com.example.comment.config.Result;
import com.example.comment.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author kuang
 * @Date 2021/7/11 18:09
 */
@RestController
@Slf4j
@RequestMapping(value = "/consumer")
public class OrderController {

    public static final String PAYMENT_URL = "http://PAYMENT";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/add")
    public Result add(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/add", payment, Result.class);
    }

    @GetMapping(value = "/get/{id}")
    public Result getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/query/" + id, Result.class);
    }

    @GetMapping(value = "/getEntity/{id}")
    public Result<Payment> getPayment2(@PathVariable("id") Long id) {
        log.info("请求参数：{}", id);
        ResponseEntity<Result> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/query/" + id, Result.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            log.info("结果：{}", entity.getBody());
            log.info(entity.getStatusCode() + "\n" + entity.getHeaders() + "\n" + entity.getBody()+"\n");
            return entity.getBody();
        } else {
            return new Result<>(444, "调用失败！");
        }
    }
}
